package com.rakuten.api.mule.transformers;

import java.math.BigDecimal;
import java.util.ArrayList;

import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.api.transport.PropertyScope;
import org.mule.transformer.AbstractMessageTransformer;

import com.rakuten.api.mule.CommonFlowConstants;
import com.rakuten.api.mule.model.rms.DisplayStartDate;
import com.rakuten.api.mule.model.rms.Inventory;
import com.rakuten.api.mule.model.rms.MarketplaceRakutenCategory;
import com.rakuten.api.mule.model.rms.Price;
import com.rakuten.api.mule.model.rms.Product;
import com.rakuten.api.mule.model.rms.ProductRequest;
import com.rakuten.api.mule.model.rms.ShippingPackage;
import com.rakuten.api.mule.model.rms.ShopCategory;
import com.rakuten.api.mule.model.rms.Variant;

public class InitializationParameters extends AbstractMessageTransformer {

	public InitializationParameters() {
		super();
		// this.registerSourceType(DataTypeFactory.create(Product.class));
		// setReturnDataType(DataTypeFactory.create(ProductRequest.class));
	}

	@Override
	public Object transformMessage(MuleMessage message, String outputEncoding)
			throws TransformerException {
		// Set original payload to session variable

		try {
			com.rakuten.api.mule.model.Product product = message
					.getPayload(com.rakuten.api.mule.model.Product.class);
			// get mearchantId
			String merchantId = message
					.getInboundProperty(CommonFlowConstants.MERCHANT_ID);

			// message.setProperty(CommonFlowConstants.MERCHANT_ID, merchantId,
			// PropertyScope.SESSION);
			//
			message.setProperty(CommonFlowConstants.PRODUCT_INFO, product,
					PropertyScope.SESSION);
			Product productRMS = new Product();
			productRMS = convertProductToProductRMS(product);

			ProductRequest productRequest = new ProductRequest();
			productRequest.setProduct(productRMS);
			productRequest.setMerchantId(merchantId);

			message.setPayload(productRequest);

			return message;
		} catch (Exception ex) {
			throw new TransformerException(this, ex);
		}
	}

	public Product convertProductToProductRMS(
			com.rakuten.api.mule.model.Product product) {
		Product productRMS = new Product();

		productRMS.setBaseSku(product.getBaseSku());
		productRMS.setBrand(product.getBrand());
		productRMS.setDescription(product.getDescriptions());
		if (product.getDisplayStartDates() != null) {
			ArrayList<DisplayStartDate> listDisplayStartDatesRMS = new ArrayList<>();
			for (com.rakuten.api.mule.model.DisplayStartDate displayStartDate : product
					.getDisplayStartDates()) {
				DisplayStartDate displayStartDateRMS = new DisplayStartDate();
				displayStartDateRMS.setDisplayStartDate(displayStartDate
						.getDisplayStartDate()
						.format("yyyy-MM-ddThh:mm:tt.zzz"));
				displayStartDateRMS.setMarketPlaceIdentifier(displayStartDate
						.getMarketplaceIdentifier());
				displayStartDateRMS.setShopUrl(displayStartDate.getShopURL());
				listDisplayStartDatesRMS.add(displayStartDateRMS);
			}
			productRMS.setDisplayStartDates(listDisplayStartDatesRMS);
		}
		productRMS.setFreeShipping(product.getFreeShipping());

		if (product.getMarketplaceRakutenCatagories() != null) {
			ArrayList<MarketplaceRakutenCategory> listMarketplaceRakutenCatagoriesRMS = new ArrayList<>();
			for (com.rakuten.api.mule.model.MarketplaceRakutenCategory marketplaceRakutenCategory : product
					.getMarketplaceRakutenCatagories()) {
				MarketplaceRakutenCategory marketplaceRakutenCategoryRMS = new MarketplaceRakutenCategory();
				marketplaceRakutenCategoryRMS
						.setMarketPlaceIdentifier(marketplaceRakutenCategory
								.getMarketplaceIdentifier());
				marketplaceRakutenCategoryRMS
						.setMarketplaceRakutenCategoryId(marketplaceRakutenCategory
								.getMarketplaceRakutenCategoryId());
				listMarketplaceRakutenCatagoriesRMS
						.add(marketplaceRakutenCategoryRMS);
			}
			productRMS
					.setMarketplaceRakutenCatagories(listMarketplaceRakutenCatagoriesRMS);
		}

		if (product.getShopCatagories() != null) {
			ArrayList<ShopCategory> listShopCategoryRMS = new ArrayList<>();
			for (com.rakuten.api.mule.model.ShopCategory shopCategory : product
					.getShopCatagories()) {
				ShopCategory shopCategoryRMS = new ShopCategory();
				shopCategoryRMS.setMarketPlaceIdentifier(shopCategory
						.getMarketplaceIdentifier());
				shopCategoryRMS.setShopCategoryIds(shopCategory
						.getShopCategoryNames());
				shopCategoryRMS.setShopUrl(shopCategory.getShopURL());

				listShopCategoryRMS.add(shopCategoryRMS);
			}
			productRMS.setShopCatagories(listShopCategoryRMS);
		}
		productRMS.setTagline(product.getTagline());
		productRMS.setTitle(product.getTitles());
		productRMS.setVariantAttributeName1(product.getVariantAttributeName1());
		productRMS.setVariantAttributeName2(product.getVariantAttributeName2());
		productRMS.setVideoURL(productRMS.getVideoURL());

		if (product.getVariants() != null) {
			ArrayList<Variant> listVariantRMS = new ArrayList<>();
			for (com.rakuten.api.mule.model.Variant variant : product
					.getVariants()) {
				Variant variantRMS = new Variant();
				variantRMS.setAttribute1(variant.getAttribute1());
				variantRMS.setAttribute2(variant.getAttribute2());
				variantRMS.setGlobalTradeItemNumber(variant
						.getGlobalTradeItemNumber());
				variantRMS.setGlobalTradeItemNumberType(variant
						.getGlobalTradeItemNumberType());
				variantRMS.setSku(variant.getSku());

				ArrayList<Inventory> listInventoryRMS = new ArrayList<>();
				for (com.rakuten.api.mule.model.Inventory inventory : variant
						.getInventories()) {
					Inventory inventoryRMS = new Inventory();
					inventoryRMS.setLimitPurchaseQuantity(inventory
							.getLimitPurchaseQuantity());
					inventoryRMS.setMarketPlaceIdentifier(inventory
							.getMarketplaceIdentifier());
					inventoryRMS.setQuantity(inventory.getQuantity());
					inventoryRMS
							.setReturnProductsToInventoryOnCanceledOrders(inventory
									.isReturnProductsToInventoryOnCanceledOrders());
					inventoryRMS.setShopUrl(inventory.getShopURL());
					listInventoryRMS.add(inventoryRMS);
				}
				variantRMS.setInventories(listInventoryRMS);
				ArrayList<Price> listPricesRMS = new ArrayList<Price>();
				if (variant.getListPrices() != null) {
					for (com.rakuten.api.mule.model.Price price : variant
							.getListPrices()) {
						Price priceRMS = new Price();
						priceRMS.setMarketPlaceIdentifier(price
								.getMarketplaceIdentifier());
						priceRMS.setShopUrl(price.getShopURL());
						if (price.getValue() != null
								&& price.getValue().length() != 0) {
							priceRMS.setValue(BigDecimal.valueOf(Double
									.valueOf(price.getValue())));
						}
						listPricesRMS.add(priceRMS);
					}
					variantRMS.setListPrices(listPricesRMS);
				}
				if (variant.getPrices() != null) {
					ArrayList<Price> pricesRMS = new ArrayList<Price>();
					for (com.rakuten.api.mule.model.Price price : variant
							.getPrices()) {
						Price priceRMS = new Price();
						priceRMS.setMarketPlaceIdentifier(price
								.getMarketplaceIdentifier());
						priceRMS.setShopUrl(price.getShopURL());
						if (price.getValue() != null
								&& price.getValue().length() != 0) {
							priceRMS.setValue(BigDecimal.valueOf(Double
									.valueOf(price.getValue())));
						}
						pricesRMS.add(priceRMS);
					}
					variantRMS.setPrices(listPricesRMS);
				}
				if (variant.getShippingPackage() != null) {
					ShippingPackage shippingPackageRMS = new ShippingPackage();
					shippingPackageRMS.setHeight(variant.getShippingPackage()
							.getHeight());
					shippingPackageRMS.setLength(variant.getShippingPackage()
							.getLength());
					shippingPackageRMS.setWeight(variant.getShippingPackage()
							.getWeight());
					shippingPackageRMS.setWidth(variant.getShippingPackage()
							.getWidth());
					variantRMS.setShippingPackage(shippingPackageRMS);
				}
			}
			productRMS.setVariant(listVariantRMS);
		}

		return productRMS;
	}

}
